// pages/address/index.js
var url = getApp().globalData.ApiUrl;
var key = wx.getStorageSync('key');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    telNumber: '',
    provinceName: '',
    cityName: '',
    countyName: '',
    detailInfo: '',
    wxaddr: false,
    address_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    
    var that = this;
    // 获取收货地址列表
    wx.request({
      url: url + '/mobile/index.php?act=member_address&op=address_list',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        key: key
      },
      success: (res) => {
        if(res.data.code == 200) {
          var address_list = res.data.datas.address_list;
          if (address_list.length > 0) {
            that.setData({
              address_list: address_list
            })
          }
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  
  // 修改收货地址
  changeAsDefault: function (e){
    var that = this;
    var dataset = e.currentTarget.dataset;
    wx.request({
      url: url + '/mobile/index.php?act=member_address&op=address_edit',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'post',
      data: {
        key: key,
        address_id: dataset.address_id,
        true_name: dataset.true_name,
        city_id: dataset.city_id,
        area_id: dataset.area_id,
        area_info: dataset.area_info,
        address: dataset.address,
        tel_phone: dataset.tel_phone,
        mob_phone: dataset.mob_phone,
        is_default: true
      },
      success: (res)=>{
        if(res.data.code == 200){
          that.onShow();
          wx.navigateBack({
            delta: 1
          });
        } else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: ()=>{
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });
    
  }
})